#include <QApplication>
#include <QLocale>
#include <QString>
#include "raycimainwindow.h"
#include <windows.h>
#include <iostream>
#include <GL\freeglut.h>
PROCESS_INFORMATION pi;

bool CreateHiddenProcess(const std::string& exePath)
{
    STARTUPINFOA si;

    ZeroMemory(&si, sizeof(si));
    si.cb = sizeof(si);
    si.dwFlags = STARTF_USESHOWWINDOW;
    si.wShowWindow = SW_HIDE;  // Hide the window
    ZeroMemory(&pi, sizeof(pi));

    if (!CreateProcessA(
            exePath.c_str(),   // Path to executable (LPCSTR)
            nullptr,              // Command line arguments (LPSTR)
            nullptr,              // Process handle not inheritable (LPSECURITY_ATTRIBUTES)
            nullptr,              // Thread handle not inheritable (LPSECURITY_ATTRIBUTES)
            FALSE,             // Set handle inheritance to FALSE (BOOL)
            CREATE_NO_WINDOW,  // No creation flags (DWORD)
            nullptr,              // Use parent's environment block (LPVOID)
            nullptr,              // Use parent's starting directory (LPCSTR)
            &si,               // Pointer to STARTUPINFO structure (LPSTARTUPINFOA)
            &pi)               // Pointer to PROCESS_INFORMATION structure (LPPROCESS_INFORMATION)
            )
    {
        std::cerr << "CreateProcess failed (" << GetLastError() << ").\n";
        return false;
    }

    // Close process and thread handles
    /*CloseHandle(pi.hProcess);
    CloseHandle(pi.hThread);*/

    return true;
}

void CloseRayCiProcess()
{
    if (pi.hProcess != nullptr)
    {
        TerminateProcess(pi.hProcess, 0);
        CloseHandle(pi.hProcess);
        CloseHandle(pi.hThread);

        pi.hProcess = nullptr;
        pi.hThread = nullptr;
    }
}

int main(int argc, char *argv[]) {
    QApplication a(argc, argv);
    FreeConsole();
    glutInit(&argc, argv);
    QTranslator translator;
    const QStringList uiLanguages = QLocale::system().uiLanguages();
    try {

        std::string exePath = "C:\\Program Files\\CINOGY\\RayCi64 Pro\\RayCi.exe";
        if (CreateHiddenProcess(exePath))
        {
            std::cout << "Process created successfully.\n";
        }
        else
        {
            std::cerr << "Failed to create process.\n";
        }

        QFile qssFile(":/qdarkstyle/style.qss");
        qssFile.open(QFile::ReadOnly);
        if(qssFile.isOpen())
        {
            QString qss = QLatin1String(qssFile.readAll());
            qApp->setStyleSheet(qss);
            qssFile.close();
        }
        RayCiMainWindow w;
        w.setWindowTitle("RayCiBeamProfiler");

        const QScreen *screen = QApplication::primaryScreen();
        const QRect screenGeometry = screen->availableGeometry();

        int windowWidth = qMin(2000, int(screenGeometry.width() * 0.8));
        int windowHeight = qMin(1000, int(screenGeometry.height() * 0.8));
        w.resize(windowWidth, windowHeight);
        w.setWindowState(Qt::WindowNoState);
        w.move(screenGeometry.center() - w.rect().center());

        w.show();

        int result = a.exec();

        CloseRayCiProcess();

        return result;
    }  catch (const char *errorStr) {
        qDebug()<<errorStr;

        CloseRayCiProcess();

        const QString msg = QString("The program has crashed.\n")
                            + "Message: " + errorStr;
        QMessageBox::warning(nullptr, "Program crashed", msg);

        return 1;
    }

}
